package com.chiwen.common.utils.http;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class PayWayUtil {

    /**
     * KS_PC_PAY
     */

    public static String KS_PC_PAY(Integer money, String userId, String ip) {
        String url = "https://pay.ssl.kuaishou.com/payAPI/k/pay/kscoin/deposit/nlogin/kspay/cashier";
        // 充值余额
        JSONObject json = new JSONObject();
        json.put("ksCoin", money * 10);
        json.put("fen", money * 100);
        json.put("userId", userId);
        json.put("customize", "true");
        json.put("kpn", "KUAISHOU");
        json.put("kpf", "PC_WEB");

        Map<String, String> header = new HashMap<>();
        header.put("x-forwarded-for", ip);

        return CommonHttp.sendPost(url, String.valueOf(json), header);
    }

    /**
     * KS_PC_PAY
     */

    public static String KS_PC_PAY_pay(String merchant_id, String out_order_no, String ip) {
        String url = "https://www.kuaishoupay.com/pay/order/pc/trade/cashier";
        String param = "merchant_id=" + merchant_id + "&out_order_no=" + out_order_no;

        return CommonHttp.sendPostForm(url, param, ip);
    }




    /**
     * KS_H5_PAY
     */

    public static String KS_H5_Pay(Integer money, String token, String ip, String payway) {
        String url = "https://www.kuaishoupay.com/rest/wd/pay/kscoin/deposit/kspay/cashier?kpn=KUAISHOU&kpf=OUTSIDE_ANDROID_H5";
        // 充值余额
        JSONObject json = new JSONObject();
        json.put("source", "");
        json.put("ksCoin", money * 10);
        json.put("fen", money * 100);
        json.put("customize", "true");
        json.put("kspayProvider", payway);
        json.put("kspayProviderChannelType", "NORMAL");

        Map<String, String> header = new HashMap<>();
        header.put("x-forwarded-for", ip);
        header.put("Cookie", "kuaishou.recharge.web_st=" + token);

        return CommonHttp.sendPost(url, String.valueOf(json), header);
    }

    /**
     * KS_H5_PAY
     */

    public static String KS_PC_PAY_pay(String merchant_id, String out_order_no, String ip, Integer money, String payway) {


        String url = "https://www.kuaishoupay.com/pay/order/h5/trade/create_pay_order";
        String param = "provider=" + payway + "&";
        param += "merchant_id=" + merchant_id + "&out_order_no=" + out_order_no + "&";
//         param += "redirect_url=https://www.kuaishoupay.com/services/h5-recharge?login_from_phone=1&source=&order_id="+out_order_no+"&platform="+payway+"&amt="+money*10+"&type=1&";
        if (payway.equals("WECHAT")) {
            param += "redirect_url=https%3A%2F%2Fwww.kuaishoupay.com%2Fservices%2Fh5-recharge%3Flogin_from_phone%3D1%26source%3D%26order_id%3D" + out_order_no + "%26platform%3D" + payway + "%26amt%3D" + money * 100 + "%26type%3D1&";
        }
        if (payway.equals("ALIPAY")) {
            param += "redirect_url=https%3A%2F%2Fwww.kuaishoupay.com%2Fservices%2Fh5-recharge%3Flogin_from_phone%3D1%26order_id%3D" + out_order_no + "%26platform%3D" + payway + "%26amt%3D" + money * 100 + "%26type%3D1&";
        }
        param += "pay_amount=" + money * 100;

//        return null;
        return CommonHttp.sendPostForm(url, param, ip);
    }



    /**
     * KS_PC_PAY
     */

    public static String KS_Order_Query(String merchant_id, String out_order_no, String ip) {
        String url = "https://www.kuaishoupay.com/pay/order/pc/trade/query";
        String param = "merchant_id=" + merchant_id + "&out_order_no=" + out_order_no;

        return CommonHttp.sendPostForm(url, param, ip);
    }

}
